yuangoInfo();
function yuangoInfo() {
    Ajax({
        url: "http://localhost/h5/php/yuango.php",
        success(res) {
            sessionStorage.setItem("yuangoInfo", res);
            res = JSON.parse(res);
            sessionStorage.setItem("pageCurrrent",1)
            let coun=Math.ceil(res.length/8)
            // console.log(coun);
            sessionStorage.setItem("coun",coun)
            let result = res.slice(0, 8);
            render(result)
        }
    })
}

// 渲染数据

function render(result) {

    const tbody = document.querySelector("tbody");
    tbody.innerHTML = "";
    result.forEach(item => {
        let str = `
        <tr>
            <td>${item.y_Id}</td>
            <td>${item.y_username}</td>
            <td>${item.y_name}</td>
            <td>${item.y_gender}</td>
            <td>${item.y_age}</td>
            <td>
            <button type="button" class="btn btn-warning btn-sm" data-o='update' data-id='${item.y_Id}'  data-bs-toggle="modal" data-bs-target='#yuangoUpdate'>修改</button>
            <button type="button" class="btn btn-danger btn-sm" data-o='del' data-id='${item.y_Id}'>删除</button>
            </td>
        </tr>`
        tbody.innerHTML += str
    });
}

// 删除和修改
deleteOrUpdate();
function deleteOrUpdate() {
    const tbody = document.querySelector("tbody");
    tbody.addEventListener("click", function (e) {
        e = e || window.event;
        // console.log(e.target.dataset.id);

        // 删除
        if (e.target.dataset.o == 'del') {
            const flag = window.confirm("您确定要删除吗？？")
            if (flag) {
                Ajax({
                    url: "http://localhost/h5/php/yuangodelete.php",
                    data: { id: e.target.dataset.id },
                    success(res) {
                        // sessionStorage.getItem("yuangoInfo", res);  
                        location.reload();
                        window.alert("删除成功!!!")
                    }
                })
            }
        }

        //   修改
        if (e.target.dataset.o == 'update') {
            const flag = window.confirm("您确定要修改吗？")
            if (flag) {
                Ajax({
                    url: "http://localhost/h5/php/queryone.php",
                    data: { id: e.target.dataset.id },
                    success(res) {
                        res = JSON.parse(res);
                        let username = document.getElementById("updateyuangoUsername");
                        let password = document.getElementById("updateyuangoPassword");
                        let name = document.getElementById("updateyuangoName");
                        let age = document.getElementById("updateyuangoAge");
                        let gender = document.getElementById("updateyuangoGender");
                        let id = document.getElementById("updateyuangoId");
                        username.value = res.y_username;
                        password.value = res.y_password;
                        name.value = res.y_name;
                        age.value = res.y_age;
                        gender.value = res.y_gender;
                        id.value = res.y_Id;
                    }
                })
            }
        }

    })
}

// 实现修改功能
function updateyuangoInfo() {
    let username = document.getElementById("updateyuangoUsername").value;
    let password = document.getElementById("updateyuangoPassword").value;
    let name = document.getElementById("updateyuangoName").value;
    let age = document.getElementById("updateyuangoAge").value;
    let gender = document.getElementById("updateyuangoGender").value;
    let id = document.getElementById("updateyuangoId").value;
    if (!username || !password || !name || !age || !gender) return window.alert("请输入完整数据!!!")
    Ajax({
        url:"http://localhost/h5/php/yuangoupdate.php",
        type: "POST",
        data: { username, password, name, age, gender,id},
        success(res) {
            sessionStorage.setItem("allyuangoInfo",res);
            location.reload();
            window.alert("修改成功!!!")
        }
    })
}

/* 实现添加用户信息 */
function addyuangoInfo() {
    let username = document.getElementById("addyuangoUsername").value;
    let password = document.getElementById("addyuangoPassword").value;
    let name = document.getElementById("addyuangoName").value;
    let age = document.getElementById("addyuangoAge").value;
    let gender = document.querySelectorAll(".addyuangoGender")
    let img='img3.jpeg'
    if (gender[0].checked) {
        gender = '男'
    } else {
        gender = '女'
    }
    if (!username || !password || !name || !age) return window.alert("请输入完整数据!!!")
    Ajax({
        url: "http://localhost/h5/php/yuangoAdd.php",
        type: "post",
        data: { username, password, name, age, gender ,img},
        success(res) {
            location.reload();
            window.alert("添加成功!!!")
        }
    })
}
/* 首页 */
function firstPage(){
    //设置当前页为第一页
    sessionStorage.setItem("pageCurrent",1);
    let result=JSON.parse(sessionStorage.getItem("yuangoInfo"))
    // console.log(result);
    result=result.slice(0,8);
    render(result)
}
// 上一页
function upPage(){
     //设置当前页为第一页
    let pageCurrent=+sessionStorage.getItem("pageCurrent")-1;
    let yuango=JSON.parse(sessionStorage.getItem("yuangoInfo"));
    if(pageCurrent<1){pageCurrent=1};
    sessionStorage.setItem("pageCurrent",pageCurrent);
    let result=yuango.slice((pageCurrent-1)*8,pageCurrent*8);
    render(result)
}
// 下一页
function downPage(){
     //设置当前页为第一页
    let pageCurrent=+sessionStorage.getItem("pageCurrent")+1;

    let yuango=JSON.parse(sessionStorage.getItem("yuangoInfo"));
    // console.log(yuango);
    let coun=+sessionStorage.getItem("coun");
    // console.log(coun);
    if(pageCurrent > coun){pageCurrent = coun};

    sessionStorage.setItem("pageCurrent",pageCurrent);

    let result=yuango.slice((pageCurrent-1)*8,pageCurrent*8);
    render(result)
}
/* 尾页 */
function lastPage(){
    //获取最后的一页
    let coun=+sessionStorage.getItem("coun");
      //当前页就是尾页
    sessionStorage.setItem("pageCurrent",coun);
     //所有的学生信息
    let yuango=JSON.parse(sessionStorage.getItem("yuangoInfo"));
    let result=yuango.slice((coun-1)*8);
    render(result)
}
// 根据性别找员工
function getGender(gender){
    Ajax({
        url:"http://localhost/h5/php/yuangogender.php",
        data:{gender},
        success(res){
            // console.log(JSON.parse(res));
            sessionStorage.setItem("yuangoInfo",res);
            // 覆盖总页数
            sessionStorage.setItem("coun",Math.ceil(JSON.parse(res).length/8));
            let result=JSON.parse(res).slice(0,8);
            render(result);
        }
    })
}
// 模糊查询
function getname(){
    let name=document.getElementById("namemo").value;
    if(name.trim().length===0){
       return window.alert("请输入关键字!!!")
    }
    Ajax({
        url:"http://localhost/h5/php/yuangoname.php",
        data:{name},
        success(res){
             console.log(JSON.parse(res));
            sessionStorage.setItem("yuangoInfo",res);
            sessionStorage.setItem("coun",Math.ceil(JSON.parse(res).length/8));
            let result=JSON.parse(res).slice(0,8);
            render(result);
        }
    })
}
